package abredatos.playas.indexer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;

public class Indexer {

	private static final String solrServerUrl = "http://localhost:8080/solr/core0";

	static CommonsHttpSolrServer solrServer = null;

	static {
		try {
			solrServer = new CommonsHttpSolrServer(solrServerUrl);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

	}

	public void readDir(String dirPath) {
		File dir = new File(dirPath);

		for (File file : dir.listFiles()) {
			try {
				String name = file.getName();
				if (name.endsWith(".xml")) {
					Playa playa = parse(file);

					name = name.replace("playa", "").replace(".xml", "");
					playa.id = name;
					index(playa);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			solrServer.commit();
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void index(Playa playa) {
		try {
			solrServer.addBean(playa);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
	}

	private Playa parse(File file) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(file));
		Playa playa = new Playa();
		String line = null;
		while ((line = br.readLine()) != null) {
			if (!line.equals("")) {
				String[] field = line.split("=");
				addField(playa, field);
			}
		}

		return playa;
	}

	private void addField(Playa playa, String[] field) {
		String fieldName = field[0].trim();
		String fieldValue = field[1].trim();

		if (fieldName.equals(" "))
			fieldName = "observaciones";
		if (fieldName.endsWith(":"))
			fieldName = fieldName.substring(0, fieldName.length() - 1);
		boolean booleanValue = false;

		// add the caracteristicas
		if (FieldType.booleanFields.contains(fieldName)) {

			if (fieldValue.toLowerCase().contains("si")) {
				booleanValue = true;
				addCaracteristicas(playa, fieldName);
			} else if (!fieldValue.toLowerCase().contains("no"))
				System.err.println("error para el campo " + fieldName + " con valor: " + fieldValue);
		}

		// set the field
		if (FieldScrapedConstants.latLng.value.equals(fieldName)) {
			playa.coords = fieldValue;
		} else if (FieldScrapedConstants.nombre.value.equals(fieldName)) {
			playa.nombre = fieldValue;
		} else if (FieldScrapedConstants.localidad.value.equals(fieldName)) {
			playa.localidad = fieldValue;
		} else if (FieldScrapedConstants.provincia.value.equals(fieldName)) {
			playa.provincia = fieldValue;
		} else if (FieldScrapedConstants.aparcamiento.value.equals(fieldName)) {
			if (fieldValue.toLowerCase().contains("si"))
				addCaracteristicas(playa, fieldName);
			playa.aparcamiento = fieldValue;
		} else if (FieldScrapedConstants.comunidad.value.equals(fieldName)) {
			playa.comunidad = fieldValue;
		} else if (FieldScrapedConstants.observaciones.value.equals(fieldName)) {
			playa.observaciones = fieldValue;
		} else if (FieldScrapedConstants.tipo.value.equals(fieldName)) {
			playa.tipo = fieldValue;
		} else if (FieldScrapedConstants.puerto.value.equals(fieldName)) {
			playa.puerto = fieldValue;
		} else if (FieldScrapedConstants.carretera.value.equals(fieldName)) {
			playa.carretera = fieldValue;
		} else if (FieldScrapedConstants.composicion.value.equals(fieldName)) {
			playa.composicion = fieldValue;
		} else if (FieldScrapedConstants.longPlaya.value.equals(fieldName)) {
			playa.longPlaya = fieldValue;
		} else if (FieldScrapedConstants.anchuraMedia.value.equals(fieldName)) {
			playa.anchuraMedia = fieldValue;
		} else if (FieldScrapedConstants.gradoOcupacion.value.equals(fieldName)) {
			playa.gradoOcupacion = fieldValue;
		} else if (FieldScrapedConstants.gradoUrbanicacion.value.equals(fieldName)) {
			playa.gradoUrbanicacion = fieldValue;
		} else if (FieldScrapedConstants.paseoMaritimo.value.equals(fieldName)) {
			playa.paseoMaritimo = fieldValue;
		} else if (FieldScrapedConstants.condicionesBano.value.equals(fieldName)) {
			playa.condicionesBano = fieldValue;
		} else if (FieldScrapedConstants.autobus.value.equals(fieldName)) {
			playa.autobus = fieldValue;
		} else if (FieldScrapedConstants.tipoAcceso.value.equals(fieldName)) {
			playa.tipoAcceso = fieldValue;
		}
		// booleans fields
		else if (FieldScrapedConstants.zonaFonde.value.equals(fieldName)) {
			playa.zonaFonde = booleanValue;
		} else if (FieldScrapedConstants.nudista.value.equals(fieldName)) {
			playa.nudista = booleanValue;
		} else if (FieldScrapedConstants.presenciaVegetacion.value.equals(fieldName)) {
			playa.presenciaVegetacion = booleanValue;
		} else if (FieldScrapedConstants.zonaProtegida.value.equals(fieldName)) {
			playa.zonaProtegida = booleanValue;
		} else if (FieldScrapedConstants.banderaAzul.value.equals(fieldName)) {
			playa.banderaAzul = booleanValue;
		} else if (FieldScrapedConstants.equipoVigilancia.value.equals(fieldName)) {
			playa.equipoVigilancia = booleanValue;
		} else if (FieldScrapedConstants.senalizacionPeligro.value.equals(fieldName)) {
			playa.senalizacionPeligro = booleanValue;
		} else if (FieldScrapedConstants.policiaLocal.value.equals(fieldName)) {
			playa.policiaLocal = booleanValue;
		} else if (FieldScrapedConstants.puestoCruzRoja.value.equals(fieldName)) {
			playa.puestoCruzRoja = booleanValue;
		} else if (FieldScrapedConstants.equpoSalvemento.value.equals(fieldName)) {
			playa.equpoSalvemento = booleanValue;
		} else if (FieldScrapedConstants.senalizacionAccesos.value.equals(fieldName)) {
			playa.senalizacionAccesos = booleanValue;
		} else if (FieldScrapedConstants.accesosMinusvalidos.value.equals(fieldName)) {
			playa.accesosMinusvalidos = booleanValue;
		} else if (FieldScrapedConstants.oficinaTurismo.value.equals(fieldName)) {
			playa.oficinaTurismo = booleanValue;
		} else if (FieldScrapedConstants.aseos.value.equals(fieldName)) {
			playa.aseos = booleanValue;
		} else if (FieldScrapedConstants.duchas.value.equals(fieldName)) {
			playa.duchas = booleanValue;
		} else if (FieldScrapedConstants.papeleras.value.equals(fieldName)) {
			playa.papeleras = booleanValue;
		} else if (FieldScrapedConstants.servicioLimpieza.value.equals(fieldName)) {
			playa.servicioLimpieza = booleanValue;
		} else if (FieldScrapedConstants.pasarelaAcceso.value.equals(fieldName)) {
			playa.pasarelaAcceso = booleanValue;
		} else if (FieldScrapedConstants.alquilerSombrilla.value.equals(fieldName)) {
			playa.alquilerSombrilla = booleanValue;
		} else if (FieldScrapedConstants.alquilerHamacas.value.equals(fieldName)) {
			playa.alquilerHamacas = booleanValue;
		} else if (FieldScrapedConstants.alquilerNauticos.value.equals(fieldName)) {
			playa.alquilerNauticos = booleanValue;
		} else if (FieldScrapedConstants.kioskosBalnearios.value.equals(fieldName)) {
			playa.kioskosBalnearios = booleanValue;
		} else if (FieldScrapedConstants.clubNautico.value.equals(fieldName)) {
			playa.clubNautico = booleanValue;
		} else if (FieldScrapedConstants.zonaSubmarinismo.value.equals(fieldName)) {
			playa.zonaSubmarinismo = booleanValue;
		}

	}

	private void addCaracteristicas(Playa playa, String fieldName) {
		if (playa.caracteristicas == null) {
			playa.caracteristicas = new ArrayList<String>();
		}
		playa.caracteristicas.add(fieldName);
	}
}
